<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty {link_picture} function plugin
 *
 * Type:     function<br>
 * Name:     link_picture<br>
 * Date:     Jan 17, 2008<br>
 * Purpose:  rewrite picture links<br>
 * @author   leo tan <tanjnr at gmail dot com>
 * @version  1.0
 * @param array
 * @param Smarty
 * @return string
 */
function smarty_function_link_picture($params, &$smarty)
{
	global $urlconf,$sysconf,$_SERVER;
	$string = $params['seroot'] ? $params['seroot'].'/'.$params['string'] : $params['string'];
	if ($sysconf['photoprotect'] && function_exists('imagecreatefromgif') && function_exists('imagecreatefromjpeg'))
	{
		$key = md5(ONLINEIP."\t".$_SERVER['HTTP_USER_AGENT']);
		$coded = '';
		$keylength = strlen($key);
		for($i = 0; $i < strlen($string); $i += $keylength) 
		{
			$coded .= substr($string, $i, $keylength) ^ $key;
		}
		$coded = str_replace('=', '', base64_encode($coded));
		if ($sysconf['urlrewrite'])
		{
			return $urlconf['domains'].'image_'.$coded.$sysconf['htmlextent'];
		}
		else
		{
			return $urlconf['photoid'].'?image='.$coded;
		}
	}
	else
	{
		return MCMS_ROOT.'upload/'.$string;
	}
}
/* vim: set expandtab: */

?>
